package com.shuqi.activity.adapter;

import java.util.ArrayList;

import com.shuqi.controller.R;
import com.umeng.socialize.bean.SHARE_MEDIA;

import android.content.Context;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ShareDialogAdapter extends BaseAdapter {

    private Context context;
    
    private ArrayList<ShareEntity> platforms=new ArrayList<ShareEntity>();
    
    public ShareDialogAdapter(Context context,boolean isWXSurported) {
        this.context=context;
        if(isWXSurported) {
            platforms.add(new ShareEntity(R.drawable.share_icon_weixin, "微信", SHARE_MEDIA.WEIXIN));
            platforms.add(new ShareEntity(R.drawable.share_icon_friend, "朋友圈", SHARE_MEDIA.WEIXIN_CIRCLE));
        }
        platforms.add(new ShareEntity(R.drawable.share_icon_weibo, "新浪微博", SHARE_MEDIA.SINA));
        platforms.add(new ShareEntity(R.drawable.share_icon_txweibo, "腾讯微博", SHARE_MEDIA.TENCENT));
        platforms.add(new ShareEntity(R.drawable.share_icon_more, "更多", SHARE_MEDIA.SMS));
    }
    
    @Override
    public int getCount() {
        return platforms.size();
    }

    @Override
    public Object getItem(int position) {
        return platforms.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        System.out.println("[gaopeng812]"+position);
        ViewHolder holder;
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.item_sharedialogadapter_gridview, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.image.setImageResource(platforms.get(position).getIcon());
        holder.text.setText(platforms.get(position).getTitle());
        return view;
    }
    
    static class ViewHolder {
        private ImageView image;
        private TextView text;
        ViewHolder(View view) {
            this.image = (ImageView) view.findViewById(R.id.share_dialog_item_image);
            this.text = (TextView) view.findViewById(R.id.share_dialog_item_text);
        }
    }

    public OnTouchListener onTouchListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    changeLight((ImageView) view, -80);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    changeLight((ImageView) view, 0);
                    break;
            }  
            return false;
        }
        
    };
    
    private void changeLight(ImageView imageview, int brightness) {
        ColorMatrix matrix = new ColorMatrix();
        matrix.set(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0,
                brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 });
        imageview.setColorFilter(new ColorMatrixColorFilter(matrix));
    }
    
    /**
     * 分享实体类
     * @author GaoPeng
     *
     */
    public class ShareEntity {
        
        // 分享平台图标
        int icon;
        // 分享平台名称
        String title;
        // 分享平台媒体
        SHARE_MEDIA media;
        
        ShareEntity(int icon,String title,SHARE_MEDIA media) {
            this.icon=icon;
            this.title=title;
            this.media=media;
        }
        
        public int getIcon() {
            return this.icon;
        }
        public String getTitle() {
            return this.title;
        }
        public SHARE_MEDIA getMedia() {
            return this.media;
        }
    }
}
